import { ListNode } from '../linkedLists.js'

var partition = function (arr, x) {
  let linkedList = new ListNode()
  let head = linkedList.create(arr)

  let dummy1 = new ListNode(null)
  let dummy2 = new ListNode(null)

  let p1 = dummy1
  let p2 = dummy2
  let p = head

  while (p !== null) {
    if (p.val < x) {
      p1.next = p
      p1 = p1.next
    } else {
      p2.next = p
      p2 = p2.next
    }
    // 不能直接让 p 指针前进，
    // p = p.next
    // 断开原链表中的每个节点的 next 指针
    let temp = p.next
    p.next = null
    p = temp
  }

  p1.next = dummy2.next
  return dummy1.next
};
let a = new ListNode()
let display = a.display(partition([1, 4, 3, 2, 5, 2], 3))
console.log(display);